//
//  BGXinYuTableViewController.m
//  bigMall
//
//  Created by ios on 16/7/19.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGXinYuTableViewController.h"
#import "BGXinYuTableViewCell.h"
#import "BGCapitalTableViewCell.H"
#import "BGDateResult.h"
#import "BGOwnCreditListResultModel.h"
#import "BGOwnCreditListModel.h"

@interface BGXinYuTableViewController () {
    
    NSMutableArray *_dataArr;
    int index;
    UIImageView *imageView;
    UILabel *lable;
    
}

@end

@implementation BGXinYuTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"我的信誉";
    
    _dataArr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //刷新的加载
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [self.tableView.header beginRefreshing];
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, (self.tableView.height - 109 - 160) / 3, 120, 160)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    [self.tableView addSubview:lable];
    
    [self.tableView addSubview:imageView];
    imageView.hidden = YES;
    lable.hidden = YES;
    
}

- (void)addIndex {
    
    index++;
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}
- (void)loadInter {
    
    [BGDateResult ownCreditListWithThisPage:[NSString stringWithFormat:@"%d",index] pageNum:@"10" success:^(NSObject *result) {
        
        BGOwnCreditListResultModel *model = [[BGOwnCreditListResultModel alloc] init];
        model = (BGOwnCreditListResultModel *)result;
        NSArray *listArr = [NSArray arrayWithArray:model.list];
        
        for (BGOwnCreditListModel *model in listArr) {
            [_dataArr addObject:model];
        }
        
        if (_dataArr.count == 0 && index == 1) {
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshingWithNoMoreData];
            
            
            imageView.hidden = NO;
            lable.hidden = NO;
            //判断数据源有无
            
           
            
        }

        if ([model.allPage intValue] > index) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
        }
//        _dataArr = listArr;
        
        if (listArr.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
            return;
        }
        imageView.hidden = YES;
        lable.hidden = YES;
        [self.tableView.footer endRefreshing];
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArr.count;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 127;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    [tableView registerNib:[UINib nibWithNibName:@"BGCapitalTableViewCell" bundle:nil] forCellReuseIdentifier:@"celId"];
    
    BGCapitalTableViewCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    
    BGOwnCreditListModel *model = [[BGOwnCreditListModel alloc] init];
    
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        
        NSDate *dat = [NSDate dateWithTimeIntervalSince1970:[model.add_time intValue]];
        NSDateFormatter*df = [[NSDateFormatter alloc]init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *str = [df stringFromDate:dat];
        cel.time.text =  str;
        
        cel.leixing.text = @"信誉：";
        cel.money.text = model.credit;
        cel.order.text = model.remark;
        cel.type.text = model.type;
        
    }

    
    // Configure the cell...
    
    return cel;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
